package com.projectx.action.chat.DevoteActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.projectx.R;
import com.projectx.action.chat.SingleChatActivity;
import com.projectx.util.ActivityManage;

import java.util.ArrayList;
import java.util.List;

public class AddDevoteActivity extends AppCompatActivity {
    private List<String> data;
    private String content;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityManage.addActivity("AddDevoteActivity", this);
        setContentView(R.layout.activity_adddevote);
        final ImageView back = findViewById(R.id.back);
        final TextView change = findViewById(R.id.change);
        final RecyclerView recyclerView = findViewById(R.id.recycler_view);
        final EditText editText = findViewById(R.id.edittext);

        try {
            getSupportActionBar().hide(); //去除标题栏
        } catch (NullPointerException e) {
            Log.d("KMH", "没有状态栏标题");
        }
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        final AddDevoteAdapter addDevoteAdapter = new AddDevoteAdapter(this);
        recyclerView.setAdapter(addDevoteAdapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityManage.exitActivity("AddDevoteActivity");
            }
        });
        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count=0;
                if (content.equals("")){
                    Toast.makeText(AddDevoteActivity.this,"请填写投票主题",Toast.LENGTH_SHORT).show();
                    count++;
                }
                for (int i = 0; i < addDevoteAdapter.getItemCount()+100; i++) {
                    try {
                        View view = linearLayoutManager.findViewByPosition(i);
                        EditText editText1 = view.findViewById(R.id.edittext);
                        Log.d("AddDevoteActivity", editText1.getText().toString());
                        if (editText1.getText().toString().equals("")&&(!editText1.getHint().toString().equals("添加选项"))) {
                            Toast.makeText(AddDevoteActivity.this, "有选项未填", Toast.LENGTH_SHORT).show();
                            count++;
                            break;
                        } else
                            data.add(editText1.getText().toString());
                    } catch (NullPointerException e) {
                        Log.d("AddDevoteActivity","数据又没了");
                        continue;
                    }
                }
                if (count==0){
                    Intent intent=new Intent(AddDevoteActivity.this,DevoteActivity.class);
                    startActivity(intent);
                }
                // Log.d("AddDevoteActivity","自闭");
            }
        });

    }
}
